package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @Author Administrator
 * @Date 2020/3/29 0029
 * @Version 1.0
 * @Description 检查组控制器
 */
@RestController
@RequestMapping("checkgroup")
public class CheckGroupController {

    //    依赖注入，调用service
    @Reference
    private CheckGroupService checkGroupService;

    /**
     * 分页查询
     */
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = null;
        /**
         * queryPageBean.getCurrentPage  总页数
         * queryPageBean.getPageSize 每页显示条数
         * queryPageBean.getQueryString 查询条件
         */
        try {
            pageResult = checkGroupService.pageQuery(queryPageBean);
            System.out.println(pageResult);
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            return pageResult = new PageResult(0L, null);
        }
    }

    /**
     * @param checkGroup   新增窗口中检查组中的基本信息
     * @param checkitemIds 新增窗口中 检查项id数组多个！
     * @return
     */
    //检查组数据添加
    @RequestMapping("add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        System.out.println(checkGroup);
        try {
            checkGroupService.add(checkGroup, checkitemIds);
            return new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.ADD_CHECKGROUP_FAIL);
        }

    }

    //根据前端传来的id，进行数据查询，用于页面数据回显
    @RequestMapping("findId")
    public Result findId(Integer id) {
        try {
            CheckGroup checkGroup = checkGroupService.findId(id);
            return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS, checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConst.QUERY_CHECKITEM_FAIL);
        }
    }


    //   基于检查组id 查询检查项id列表
    @RequestMapping("findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id) {
        try {
//            因为是获取检查项列表，存在多条数据，使用list数组
            List<Integer> integers = checkGroupService.findCheckItemIdsByCheckGroupId(id);
            System.out.println(integers);
            return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS, integers);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConst.QUERY_CHECKITEM_FAIL);
        }
    }

    //    编辑信息
    @RequestMapping("update")
    public Result update(@RequestBody CheckGroup checkGroup, Integer[] id) {
        try {
            checkGroupService.update(checkGroup, id);
            return new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConst.ADD_CHECKGROUP_FAIL);
        }

    }

    @RequestMapping("delete")
    public Result delete(Integer id) {
        //查询checkgroupid在关联表中是否存在关联关系，不存在直接删除，反之不允许删除
        List<Integer> list = checkGroupService.findCheckItemIdsByCheckGroupId(id);
        if (list.isEmpty()) {
            checkGroupService.delete(id);
            return new Result(true, MessageConst.DELETE_CHECKGROUP_SUCCESS);
        } else {
            return new Result(true, MessageConst.DELETE_CHECKGROUP_FAIL);
        }
    }
    @RequestMapping("findAll")
    public Result findAll() {
        //查询checkgroupid在关联表中是否存在关联关系，不存在直接删除，反之不允许删除
        try {
            List<CheckGroup> list = checkGroupService.findAll();
            return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConst.QUERY_CHECKGROUP_FAIL);

        }
    }

}
